package com.imooc.course.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.course.server.dao.RoleResourceDao;
import com.imooc.course.server.dao.my.MyRoleResourceDao;
import com.imooc.course.server.dto.RoleResourceDto;
import com.imooc.course.server.dto.PageDto;
import com.imooc.course.server.entity.RoleResource;
import com.imooc.course.server.entity.RoleResourceExample;
import com.imooc.course.server.service.RoleResourceService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service("roleResourceService")
public class RoleResourceServiceImpl implements RoleResourceService {

    private static final Logger log = LoggerFactory.getLogger(RoleResourceServiceImpl.class);

    @Resource(name = "roleResourceDao")
    private RoleResourceDao roleResourceDao;

    @Resource(name = "myRoleResourceDao")
    private MyRoleResourceDao myRoleResourceDao;

    /**
     * 列表查询
     * @param pageDto
     */
    @Override
    public void getList(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());

        RoleResourceExample roleResourceExample = new RoleResourceExample();
        List<RoleResource> roleResourceList = roleResourceDao.selectByExample(roleResourceExample);

        PageInfo<RoleResource> roleResourcePageInfo = new PageInfo<>(roleResourceList);
        pageDto.setTotal(roleResourcePageInfo.getTotal());

        List<RoleResourceDto> roleResourceDtoList = CopyUtil.copyList(roleResourceList, RoleResourceDto.class);

        pageDto.setList(roleResourceDtoList);
    }

    /**
     * 保存，id有值为更新，id无值为新增
     * @param roleResourceDto
     */
    @Override
    public void save(RoleResourceDto roleResourceDto) {
        RoleResource copy = CopyUtil.copy(roleResourceDto, RoleResource.class);
        String id = roleResourceDto.getId() != null ? roleResourceDto.getId().trim() : roleResourceDto.getId();
        if (StringUtils.isEmpty(id)) {
            this.addRoleResource(copy);
        }else{
            this.updateRoleResource(copy);
        }
    }

    private void updateRoleResource(RoleResource copy) {
        roleResourceDao.updateByPrimaryKeySelective(copy);
    }

    private void addRoleResource(RoleResource copy) {
        copy.setId(UuidUtil.getShortUuid());
        roleResourceDao.insertSelective(copy);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id) {
        roleResourceDao.deleteByPrimaryKey(id);
    }


    /**
     * 授予角色权限
     * @param roleResourceDto
     */
    @Transactional
    @Override
    public void authorization(RoleResourceDto roleResourceDto) {

        String roleId = roleResourceDto.getRoleId();
        List<String> resourceIdList = roleResourceDto.getResourceIdList();

        RoleResourceExample example = new RoleResourceExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleResourceDao.deleteByExample(example);

        if (CollectionUtils.isEmpty(resourceIdList)) {
            return ;
        }

        List<RoleResource> list = resourceIdList.stream()
                .map((id) -> {
                    RoleResource roleResource = new RoleResource();
                    roleResource.setId(UuidUtil.getShortUuid());
                    roleResource.setRoleId(roleId);
                    roleResource.setResourceId(id);
                    return roleResource;
                })
                .collect(Collectors.toList());

        myRoleResourceDao.batchInsert(list);

    }

    @Override
    public List<RoleResourceDto> checkNode(String id) {
        RoleResourceExample example = new RoleResourceExample();
        example.createCriteria().andRoleIdEqualTo(id);
        List<RoleResource> roleResourceList = roleResourceDao.selectByExample(example);
        return CopyUtil.copyList(roleResourceList, RoleResourceDto.class);
    }


}
